package com.example.xinlijiankangsys.mapper;
import com.example.xinlijiankangsys.pojo.UserInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;

@Mapper
public interface UserInfoMapper {
    @Results({
            @Result(property = "userId", column = "user_id"),
    })
    @Select("select * from xinlijiankang.user_info where user_id=#{userId}")
    UserInfo getUserInfo(String userId);

    @Results({
            @Result(property = "userId", column = "user_id"),
    })
    @Insert("insert into xinlijiankang.user_info(user_id) values(#{userId})")
    int insertUserInfo(UserInfo userInfo);

    @Results({
            @Result(property = "userId", column = "user_id"),
    })
    @Update("update xinlijiankang.user_info set name=#{name},sex=#{sex},age=#{age},phone=#{phone},email=#{email},address=#{address} where user_id=#{userId}")
    int updateUserInfo(UserInfo userInfo);

    /**
     * 根据用户ID查询基本信息
     *
     * @param userId 用户ID
     * @return 用户信息Map（仅包含user_info表字段）
     */
    @Select("SELECT user_id, name, avatar_path FROM xinlijiankang.user_info WHERE user_id = #{userId}")
    Map<String, Object> getUserBasicInfo(@Param("userId") String userId);

    @Select("SELECT " +
            "u.user_id, " +
            "u.password, " +
            "u.auth, " +
            "i.name, " +
            "i.avatar_path, " +
            "CASE " +
            "  WHEN u.auth = 1 THEN '学生' " +
            "  WHEN u.auth = 2 THEN '心理咨询师' " +
            "  ELSE '辅导员' " +
            "END AS auth_name " +
            "FROM user_tb u " +
            "JOIN user_info i ON u.user_id = i.user_id " +
            "WHERE u.user_id = #{userId}")
    Map<String, Object> getUserAsMap(@Param("userId") String userId);


    /**
     * 模糊查询user_id（保持原功能）
     */
    @Select({
            "SELECT user_id, name  FROM xinlijiankang.user_info",
            "WHERE user_id LIKE CONCAT('%', #{keyword}, '%')",
            "OR name LIKE CONCAT('%', #{keyword}, '%')"
    })
    List<Map<String, Object>> searchUsers(@Param("keyword") String keyword);
}